﻿using System;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Collections.Generic;
using System.Reflection;
namespace THS.HtmlHelpers
{
    public static class PagingHelpers
    {
        public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl)
        {
            StringBuilder result = new StringBuilder();
            for (int i = 1; i <= pagingInfo.TotalPages; i++)
            {
                TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag
                tag.MergeAttribute("href", pageUrl(i));
                tag.InnerHtml = i.ToString();
                if (i == pagingInfo.CurrentPage)
                    tag.AddCssClass("selected");
                result.AppendLine(tag.ToString());
            }
            MvcHtmlString htmtring = MvcHtmlString.Create(result.ToString());
            return MvcHtmlString.Create(result.ToString());
        }
        public static MvcHtmlString Pager(this AjaxHelper helper, int CurrentPage, int TotalRecords, string TargetDiv, int id = 0, int PageSize = 10, string ActionName = "Index")
        {
            if (TotalRecords > 0)
            {
                StringBuilder sb = new StringBuilder();
                int TotalPages = (int)Math.Ceiling(TotalRecords / (double)PageSize);
                //int TotalPages =(int) TotalRecords / PageSize;
                //Build the Ajax Options
                AjaxOptions ao = new AjaxOptions() { UpdateTargetId = TargetDiv };
                if (CurrentPage >= 2)
                {
                    //Add the Back Links
                    sb.Append(AjaxExtensions.ActionLink(helper, "Đầu", ActionName, new { id = id, Page = 1 }, ao));
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, "<", ActionName, new { id = id, Page = CurrentPage - 1 }, ao));


                }
                if (CurrentPage == 2)
                {
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage - 1), ActionName, new { id = id, Page = CurrentPage - 1 }, ao));
                }

                else if (CurrentPage > 2 && CurrentPage < TotalPages)
                {
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage - 2), ActionName, new { id = id, Page = CurrentPage - 2 }, ao));
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage - 1), ActionName, new { id = id, Page = CurrentPage - 1 }, ao));
                }
                else if (CurrentPage == TotalPages && TotalPages > 1)
                {
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage - 1), ActionName, new { id = id, Page = CurrentPage - 1 }, ao));
                    sb.Append("  ");
                    sb.Append(String.Format("<a>{0}</a>", CurrentPage));
                }
                else if (CurrentPage == TotalPages && TotalPages == 1)
                {
                    sb.Append("  ");
                    sb.Append(String.Format("<a>{0}</a>", CurrentPage));
                }

                if (CurrentPage < TotalPages)
                {
                    //current page not add to link
                    sb.Append("  ");
                    sb.Append(String.Format("<a>{0}</a>", CurrentPage));
                    //region next
                    if (CurrentPage < 2)
                    {

                        for (int i = 1; i < TotalPages; i++)
                        {
                            if (i < 5)
                            {
                                sb.Append("  ");
                                sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage + i), ActionName, new { id = id, Page = CurrentPage + i }, ao));
                            }
                            else
                                break;
                        }
                    }
                    else
                    {
                        if (CurrentPage + 2 <= TotalPages)
                        {
                            sb.Append("  ");
                            sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage + 1), ActionName, new { id = id, Page = CurrentPage + 1 }, ao));
                            sb.Append("  ");
                            sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage + 2), ActionName, new { id = id, Page = CurrentPage + 2 }, ao));
                        }
                        else
                        {
                            sb.Append("  ");
                            sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage + 1), ActionName, new { id = id, Page = CurrentPage + 1 }, ao));
                        }
                    }
                    //Add the Next Links
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, ">", ActionName, new { id = id, Page = CurrentPage + 1 }, ao));
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, "Cuối", ActionName, new { id = id, Page = TotalPages }, ao));
                }

                return MvcHtmlString.Create(sb.ToString());
            }
            else
            {
                //Don't return anything for the pager if we do not have any records
                return MvcHtmlString.Create("");
            }
        }
        //
        public static MvcHtmlString Pager(this AjaxHelper helper, int CurrentPage, int TotalRecords, string TargetDiv, String Filter, int PageSize = 10, string ActionName = "Index")
        {
            if (TotalRecords > 0)
            {
                StringBuilder sb = new StringBuilder();
                int TotalPages = (int)Math.Ceiling(TotalRecords / (double)PageSize);
                //int TotalPages =(int) TotalRecords / PageSize;
                //Build the Ajax Options
                AjaxOptions ao = new AjaxOptions() { UpdateTargetId = TargetDiv };
                if (CurrentPage >= 2)
                {
                    //Add the Back Links
                    sb.Append(AjaxExtensions.ActionLink(helper, "Đầu", ActionName, new { Filter = Filter, Page = 1 }, ao));
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, "<", ActionName, new { Filter = Filter, Page = CurrentPage - 1 }, ao));


                }
                if (CurrentPage == 2)
                {
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage - 1), ActionName, new { Filter = Filter, Page = CurrentPage - 1 }, ao));
                }

                else if (CurrentPage > 2 && CurrentPage < TotalPages)
                {
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage - 2), ActionName, new { Filter = Filter, Page = CurrentPage - 2 }, ao));
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage - 1), ActionName, new { Filter = Filter, Page = CurrentPage - 1 }, ao));
                }
                else if (CurrentPage == TotalPages && TotalPages > 1)
                {
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage - 1), ActionName, new { Filter = Filter, Page = CurrentPage - 1 }, ao));
                    sb.Append("  ");
                    sb.Append(String.Format("<a>{0}</a>", CurrentPage));
                }
                else if (CurrentPage == TotalPages && TotalPages == 1)
                {
                    sb.Append("  ");
                    sb.Append(String.Format("<a>{0}</a>", CurrentPage));
                }

                if (CurrentPage < TotalPages)
                {
                    //current page not add to link
                    sb.Append("  ");
                    sb.Append(String.Format("<a>{0}</a>", CurrentPage));
                    //region next
                    if (CurrentPage < 2)
                    {

                        for (int i = 1; i < TotalPages; i++)
                        {
                            if (i < 5)
                            {
                                sb.Append("  ");
                                sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage + i), ActionName, new { Filter = Filter, Page = CurrentPage + i }, ao));
                            }
                            else
                                break;
                        }
                    }
                    else
                    {
                        if (CurrentPage + 2 <= TotalPages)
                        {
                            sb.Append("  ");
                            sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage + 1), ActionName, new { Filter = Filter, Page = CurrentPage + 1 }, ao));
                            sb.Append("  ");
                            sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage + 2), ActionName, new { Filter = Filter, Page = CurrentPage + 2 }, ao));
                        }
                        else
                        {
                            sb.Append("  ");
                            sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage + 1), ActionName, new { Filter = Filter, Page = CurrentPage + 1 }, ao));
                        }
                    }
                    //Add the Next Links
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, ">", ActionName, new { Filter = Filter, Page = CurrentPage + 1 }, ao));
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, "Cuối", ActionName, new { Filter = Filter, Page = TotalPages }, ao));
                }

                return MvcHtmlString.Create(sb.ToString());
            }
            else
            {
                //Don't return anything for the pager if we do not have any records
                return MvcHtmlString.Create("");
            }
        }
        public static MvcHtmlString Pager(this AjaxHelper helper, int CurrentPage, int TotalRecords, string TargetDiv, int id, String Filter, int PageSize = 10, string ActionName = "Index")
        {
            if (TotalRecords > 0)
            {
                StringBuilder sb = new StringBuilder();
                int TotalPages = (int)Math.Ceiling(TotalRecords / (double)PageSize);
                //int TotalPages =(int) TotalRecords / PageSize;
                //Build the Ajax Options
                AjaxOptions ao = new AjaxOptions() { UpdateTargetId = TargetDiv };
                if (CurrentPage >= 2)
                {
                    //Add the Back Links
                    sb.Append(AjaxExtensions.ActionLink(helper, "Đầu", ActionName, new { id = id, Filter = Filter, Page = 1 }, ao));
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, "<", ActionName, new { id = id, Filter = Filter, Page = CurrentPage - 1 }, ao));


                }
                if (CurrentPage == 2)
                {
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage - 1), ActionName, new { id = id,Filter=Filter, Page = CurrentPage - 1 }, ao));
                }

                else if (CurrentPage > 2 && CurrentPage < TotalPages)
                {
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage - 2), ActionName, new { id = id, Filter = Filter, Page = CurrentPage - 2 }, ao));
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage - 1), ActionName, new { id = id, Filter = Filter, Page = CurrentPage - 1 }, ao));
                }
                else if (CurrentPage == TotalPages && TotalPages > 1)
                {
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage - 1), ActionName, new { id = id, Filter = Filter, Page = CurrentPage - 1 }, ao));
                    sb.Append("  ");
                    sb.Append(String.Format("<a>{0}</a>", CurrentPage));
                }
                else if (CurrentPage == TotalPages && TotalPages == 1)
                {
                    sb.Append("  ");
                    sb.Append(String.Format("<a>{0}</a>", CurrentPage));
                }

                if (CurrentPage < TotalPages)
                {
                    //current page not add to link
                    sb.Append("  ");
                    sb.Append(String.Format("<a>{0}</a>", CurrentPage));
                    //region next
                    if (CurrentPage < 2)
                    {

                        for (int i = 1; i < TotalPages; i++)
                        {
                            if (i < 5)
                            {
                                sb.Append("  ");
                                sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage + i), ActionName, new { id = id, Filter = Filter, Page = CurrentPage + i }, ao));
                            }
                            else
                                break;
                        }
                    }
                    else
                    {
                        if (CurrentPage + 2 <= TotalPages)
                        {
                            sb.Append("  ");
                            sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage + 1), ActionName, new { id = id, Filter = Filter, Page = CurrentPage + 1 }, ao));
                            sb.Append("  ");
                            sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage + 2), ActionName, new { id = id, Filter = Filter, Page = CurrentPage + 2 }, ao));
                        }
                        else
                        {
                            sb.Append("  ");
                            sb.Append(AjaxExtensions.ActionLink(helper, String.Format("{0}", CurrentPage + 1), ActionName, new { id = id, Filter = Filter, Page = CurrentPage + 1 }, ao));
                        }
                    }
                    //Add the Next Links
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, ">", ActionName, new { id = id, Filter = Filter, Page = CurrentPage + 1 }, ao));
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, "Cuối", ActionName, new { id = id, Filter = Filter, Page = TotalPages }, ao));
                }

                return MvcHtmlString.Create(sb.ToString());
            }
            else
            {
                //Don't return anything for the pager if we do not have any records
                return MvcHtmlString.Create("");
            }
        }
        public static MvcHtmlString Pager(this AjaxHelper helper, int CurrentPage, int TotalRecords, string TargetDiv, int iGameId, int iVideoId, int iNEWSId, int PageSize = 10, string ActionName = "Index")
        {
            if (TotalRecords > 0)
            {
                StringBuilder sb = new StringBuilder();
                int TotalPages = (int)Math.Ceiling(TotalRecords / (double)PageSize);
                //int TotalPages =(int) TotalRecords / PageSize;
                //Build the Ajax Options
                AjaxOptions ao = new AjaxOptions() { UpdateTargetId = TargetDiv };

                if (CurrentPage > 0)
                {


                    //Add the Back Links
                    sb.Append(AjaxExtensions.ActionLink(helper, "<<", ActionName, new { iGameId = iGameId, iVideoId = iVideoId, iNEWSId = iNEWSId, Page = 0 }, ao));
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, "<", ActionName, new { iGameId = iGameId, iVideoId = iVideoId, iNEWSId = iNEWSId, Page = CurrentPage - 1 }, ao));
                    sb.Append("  ");
                }

                //Add the Page Number
                sb.Append(String.Format("{0} / {1}", CurrentPage + 1, TotalPages));

                if (CurrentPage < (TotalPages - 1))
                {
                    //Add the Next Links
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, ">", ActionName, new { iGameId = iGameId, iVideoId = iVideoId, NewsId = iNEWSId, Page = CurrentPage + 1 }, ao));
                    sb.Append("  ");
                    sb.Append(AjaxExtensions.ActionLink(helper, ">>", ActionName, new { iGameId = iGameId, iVideoId = iVideoId, NewsId = iNEWSId, Page = TotalPages - 1 }, ao));
                }

                return MvcHtmlString.Create(sb.ToString());
            }
            else
            {
                //Don't return anything for the pager if we do not have any records
                return MvcHtmlString.Create("");
            }
        }
        //
        public static MvcHtmlString ImagePager(this AjaxHelper helper, int CurrentPage, int TotalRecords, string TargetDiv, String imageUrlPrv, String altPrv, String imageUrlNext, String altNext, int id = 0, int PageSize = 10, string ActionName = "Index")
        {
            if (TotalRecords > 0)
            {
                StringBuilder sb = new StringBuilder();
                int TotalPages = (int)Math.Ceiling(TotalRecords / (double)PageSize);
                //int TotalPages =(int) TotalRecords / PageSize;
                //Build the Ajax Options
                AjaxOptions ao = new AjaxOptions() { UpdateTargetId = TargetDiv };
                int NextPage = CurrentPage;
                int PrevPage = CurrentPage;
                if (CurrentPage == 0)
                {
                    //int validatePage = 0;
                    if (TotalPages > 1)
                        NextPage++;
                    //else 
                    //    validatePage=CurrentPage;
                    var builderPrv = new TagBuilder("img");
                    builderPrv.MergeAttribute("src", imageUrlPrv);
                    builderPrv.MergeAttribute("alt", altPrv);
                    //Add the Back Links
                    var linkPrv = AjaxExtensions.ActionLink(helper, " ", ActionName, new { id = id, Page = PrevPage }, ao);
                    String sLinkPrv = linkPrv.ToString();
                    String imagPrv = sLinkPrv.Insert(sLinkPrv.Length - 4, builderPrv.ToString());
                    sb.Append(imagPrv);
                    //next
                    sb.Append(" ");
                    var builderNext = new TagBuilder("img");
                    builderNext.MergeAttribute("src", imageUrlNext);
                    builderNext.MergeAttribute("alt", altNext);

                    var linkNext = AjaxExtensions.ActionLink(helper, " ", ActionName, new { id = id, Page = NextPage }, ao);
                    String sLinkNext = linkNext.ToString();
                    String imagNext = sLinkNext.Insert(sLinkNext.Length - 4, builderNext.ToString());
                    sb.Append(imagNext);


                }

                else
                {
                    //int validatePage = 0;
                    if (CurrentPage == TotalPages - 1)
                        NextPage--;
                    //else
                    NextPage++;
                    PrevPage--;
                    var builderPrv = new TagBuilder("img");
                    builderPrv.MergeAttribute("src", imageUrlPrv);
                    builderPrv.MergeAttribute("alt", altPrv);
                    //Add the Back Links
                    var linkPrv = AjaxExtensions.ActionLink(helper, " ", ActionName, new { id = id, Page = PrevPage }, ao);
                    String sLinkPrv = linkPrv.ToString();
                    String imagPrv = sLinkPrv.Insert(sLinkPrv.Length - 4, builderPrv.ToString());
                    sb.Append(imagPrv);
                    //
                    sb.Append(" ");
                    var builderNext = new TagBuilder("img");
                    builderNext.MergeAttribute("src", imageUrlNext);
                    builderNext.MergeAttribute("alt", altNext);
                    var linkNext = AjaxExtensions.ActionLink(helper, " ", ActionName, new { id = id, Page = NextPage }, ao);
                    String sLinkNext = linkNext.ToString();
                    String imageNext = sLinkNext.Insert(sLinkNext.Length - 4, builderNext.ToString());
                    sb.Append(imageNext);
                }

                return MvcHtmlString.Create(sb.ToString());
            }
            else
            {
                //Don't return anything for the pager if we do not have any records
                return MvcHtmlString.Create("");
            }
        }


    }
}